<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>设置用户自定义的异常处理函数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.set-error-handler.html">« set_error_handler</a></li>
      <li style="float: right;"><a href="function.trigger-error.html">trigger_error »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.errorfunc.html">错误处理 函数</a></li>
    <li>设置用户自定义的异常处理函数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.set-exception-handler" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">set_exception_handler</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">set_exception_handler</span> &mdash; <span class="dc-title">
   设置用户自定义的异常处理函数
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.set-exception-handler-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>set_exception_handler</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span><span class="type"></span></span> <code class="parameter">$callback</code></span>): <span class="type"><span class="type">?</span><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span></span></div>

  <p class="para rdfs-comment">
   设置默认的异常处理程序，用于没有用 try/catch 块来捕获的异常。
   在 <code class="parameter">callback</code> 调用后异常会中止。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.set-exception-handler-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
       当一个未捕获的异常发生时所调用的函数。该处理函数需要接受一个参数，该参数是抛出的
       <span class="classname"><a href="class.throwable.html" class="classname">Throwable</a></span> 对象。<span class="classname"><a href="class.error.html" class="classname">Error</a></span>
       和 <span class="classname"><a href="class.exception.html" class="classname">Exception</a></span> 都实现了 <span class="classname"><a href="class.throwable.html" class="classname">Throwable</a></span>
       接口。这是处理程序签名：       
      </p>
      <p class="para">
       <div class="methodsynopsis dc-description">
        <span class="methodname"><span class="replaceable">handler</span></span>(<span class="methodparam"><span class="type"><a href="class.throwable.html" class="type Throwable">Throwable</a></span> <code class="parameter">$ex</code></span>): <span class="type"><span class="type void">void</span></span></div>

      </p>
      <p class="para">
        也可以传递 <strong><code>null</code></strong> 值用于重置异常处理函数为默认值。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.set-exception-handler-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回之前定义的异常处理程序，或者在错误时返回
   <strong><code>null</code></strong>。如果之前没有定义错误处理程序，也会返回 <strong><code>null</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.set-exception-handler-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-522">
    <p><strong>示例 #1 <span class="function"><strong>set_exception_handler()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">exception_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">Throwable&nbsp;$exception</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Uncaught&nbsp;exception:&nbsp;"&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$exception</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">set_exception_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">'exception_handler'</span><span style="color: #007700">);<br /><br />throw&nbsp;new&nbsp;</span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Uncaught&nbsp;Exception'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Not&nbsp;Executed\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.set-exception-handler-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.restore-exception-handler.html" class="function" rel="rdfs-seeAlso">restore_exception_handler()</a> - 恢复之前定义过的异常处理函数。</span></li>
    <li class="member"><span class="function"><a href="function.restore-error-handler.html" class="function" rel="rdfs-seeAlso">restore_error_handler()</a> - 还原之前的错误处理函数</span></li>
    <li class="member"><span class="function"><a href="function.error-reporting.html" class="function" rel="rdfs-seeAlso">error_reporting()</a> - 设置应该报告何种 PHP 错误</span></li>
    <li class="member"><a href="language.exceptions.html" class="link">PHP 5 异常</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>